package com.dpad.crmclientapp.android.modules.newapp.presenter


import com.dpad.crmclientapp.android.modules.newapp.ui.fragment.MessageFragment
import com.dpad.telematicsclientapp.mvp.kit.Kits
import com.dpad.telematicsclientapp.mvp.mvp.XPresent
import com.dpad.telematicsclientapp.netlibrary.http.NetInstance
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.OutMessageBean
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.TextMessageBean
import com.dpad.telematicsclientapp.util.utils.ParameterUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*

/**
 * ================================================
 * 作    者：booob
 * 版    本：1.0
 * 创建日期：2018-05-18-0018 17:30
 * 描    述：
 * 修订历史：
 * ================================================
 */
class MessageFragmentPresenter : XPresent<MessageFragment>() {

    fun getMessageData(map: SortedMap<String, Any>) {
        v.loading("")
        NetInstance.getEventsService().getNewMessageData(ParameterUtils.getHeaser(map), ParameterUtils.getJsonBody(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<OutMessageBean> {
                    override fun onComplete() {
                        v.loadingComplete()
                    }

                    override fun onSubscribe(d: Disposable?) {
                        addDisposable(d)
                    }

                    override fun onNext(value: OutMessageBean?) {
                        value!!.let {
                            if (value.code == "00000") {
                                v.showData(it)
                            }
                        }
                    }

                    override fun onError(e: Throwable?) {
                        v.loadingComplete()
//                        v.showNoData()
                    }

                })
    }

    /**
     *
     * @param readState 是否已读
     * @param delFlag 删除的状态
     * @param typeState 消息状态:0无效,1有效
     * @param type 那种类型(一键已读)
     * @param needRefresh 是否需要loading
     */
    fun clearMessage(readState: String, delFlag: String, typeState: String, type: String, needRefresh: Boolean) {
        val paramsMap: SortedMap<String, Any> = sortedMapOf(Pair("typeState", typeState))
        if (needRefresh) {
            v.loading("正在删除...")
        }
        paramsMap["readState"] = readState
        paramsMap["delFlag"] = delFlag
        if (!Kits.Empty.check(type)) {
            paramsMap["type"] = type
        }

        NetInstance.getEventsService().updateMessageState(ParameterUtils.getHeaser(paramsMap), ParameterUtils.getJsonBody(paramsMap))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<TextMessageBean> {
                    override fun onComplete() {
                        v.loadingComplete()
                    }

                    override fun onSubscribe(d: Disposable?) {
                        addDisposable(d)
                    }

                    override fun onNext(value: TextMessageBean?) {
                        value?.let {
                            if (value.code == "00000") {
                                v.onRefresh()
                            }
                        }

                    }

                    override fun onError(e: Throwable?) {
                        v.loadingComplete()
                    }

                })
    }
}
